<template>
  <!-- 底部 -->
  <div class="my-footer">
    <!-- 全选 -->
    <div class="custom-control custom-checkbox">
      <input
        v-model="isAll"
        type="checkbox"
        class="custom-control-input"
        id="footerCheck"
      />
      <label class="custom-control-label" for="footerCheck">全选</label>
    </div>
    <!-- 合计 -->
    <div>
      <span>合计:</span>
      <!-- 显示：选中商品的总价 -->
      <span class="price">¥ {{ seledGoodsPrices }}</span>
    </div>
    <!-- 按钮 -->
    <!-- 显示：当前选中的商品的总数量 -->
    <button type="button" class="footer-btn btn btn-primary">
      结算 ({{ seledGoodsNums }})
    </button>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      default: () => []
    },
  },
  computed: {
    isAll: {
      // 读取
      get () {
        return this.list.every(good => good.goods_state)
      },
      // 修改=》存储 =>val是全选框的值
      set (val) {
        console.log(val)
        // 修改列表数据的选中状态
        this.list.forEach(good => {
          good.goods_state = val
        })
      }
    },
    // 计算选中商品的总价
    seledGoodsPrices () {
      /**
  * 思路：
  * 1. 获取选中的商品
  * 2. 根据选中商品，累加选中商品的价格
  */
      const seledGoods = this.list.filter(good => good.goods_state)
      const total = seledGoods.reduce((acc, good) => {
        // 累加：数量*单价
        return acc += good.goods_count * good.goods_price
      }, 0)

      return total
    },
    // 计算选中商品的总数
    seledGoodsNums () {
      /**
       * 思路：
       * 1. 获取选中的商品
       * 2. 根据选中商品，累加count商品数量 
       */
      const seledGoods = this.list.filter(good => good.goods_state)
      const total = seledGoods.reduce((acc, good) => {
        return acc += good.goods_count
      }, 0)
      console.log(seledGoods, total)
      return total
    }
  },
}
</script>

<style lang="less" scoped>
.my-footer {
  position: fixed;
  z-index: 2;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  background: #fff;

  .price {
    color: red;
    font-weight: bold;
    font-size: 15px;
  }
  .footer-btn {
    min-width: 80px;
    height: 30px;
    line-height: 30px;
    border-radius: 25px;
    padding: 0;
  }
}
</style>